/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// 等待 Cordova 环境就绪
document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    console.log('Cordova 环境已就绪，可调用振动 API');
    
    // 绑定按钮点击事件
    const shortVibrateBtn = document.getElementById('shortVibrate');
    const longVibrateBtn = document.getElementById('longVibrate');
    const stopVibrateBtn = document.getElementById('stopVibrate');
    
    if (shortVibrateBtn) {
        shortVibrateBtn.addEventListener('click', () => vibrateShort());
    }
    
    if (longVibrateBtn) {
        longVibrateBtn.addEventListener('click', () => vibrateLong());
    }
    
    if (stopVibrateBtn) {
        stopVibrateBtn.addEventListener('click', () => stopVibrate());
    }
}

// 短振动（500ms）
function vibrateShort() {
    vibrate(500);
    showToast('已触发 500ms 短振动');
}

// 长振动（2000ms）
function vibrateLong() {
    vibrate(2000);
    showToast('已触发 2000ms 长振动');
}

// 停止振动
function stopVibrate() {
    vibrate(0);
    showToast('已停止当前振动');
}

// 触发振动
function vibrate(time) {
    console.log('触发振动，时长：', time, 'ms');
    
    let vibrateSuccess = false;

    // 方式1: 使用标准 API navigator.vibrate()
    if (typeof navigator.vibrate === 'function') {
        try {
            navigator.vibrate(time);
            console.log('使用 navigator.vibrate() 触发振动');
            vibrateSuccess = true;
        } catch (e) {
            console.warn('navigator.vibrate() 调用失败：', e);
        }
    }
    
    // 方式2: 使用 vibration 对象（如果方式1失败）
    if (!vibrateSuccess && typeof navigator.vibration !== 'undefined') {
        if (typeof navigator.vibration.vibrate === 'function') {
            try {
                navigator.vibration.vibrate(time);
                console.log('使用 navigator.vibration.vibrate() 触发振动');
                vibrateSuccess = true;
            } catch (e) {
                console.warn('navigator.vibration.vibrate() 调用失败：', e);
            }
        }
    }
    
    // 如果都不可用，显示提示
    if (!vibrateSuccess) {
        console.error('震动 API 不可用');
        showToast('当前设备不支持震动功能，请在真实设备上测试');
    }
}

// 显示提示消息（模拟 Toast）
function showToast(message) {
    const toast = document.getElementById('toast');
    if (!toast) return;
    
    toast.textContent = message;
    toast.style.display = 'block';
    
    setTimeout(() => {
        toast.style.display = 'none';
    }, 2000);
}

// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
    console.log('震动功能页面加载完成');
    
    // 如果不在 Cordova 环境中，显示提示
    if (typeof cordova === 'undefined') {
        console.log('检测到浏览器环境，震动功能需要在设备上测试');
    }
});
